package ge.info.domain.transport;

import ge.ktapestry.propertyselections.ComboList;
import ge.ktapestry.propertyselections.IComboItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Mar 26, 2007
 * Time: 9:39:09 PM
 * To change this template use File | Settings | File Templates.
 */
public class DayOfWeek implements java.io.Serializable, IComboItem
{
    private int dayID;
    private String day;

    private static ComboList days;
    private static int[] dayArray = {0, 1, 2, 4, 8, 16, 32, 64};
    private static List<String> dayStringArray ;

    @Override
    public boolean equals(Object o)
    {
        if (!(o instanceof DayOfWeek))
        {
            return false;
        }
        return this.getDayID() == (((DayOfWeek) o).getDayID());
    }

    public String getUniqueID()
    {
        return String.valueOf(getDayID());
    }

    public String getDisplayName()
    {
        return getDay();
    }

    public int getDayID()
    {
        return dayID;
    }

    public void setDayID(int dayID)
    {
        this.dayID = dayID;
    }

    public String getDay()
    {
        return day;
    }

    public void setDay(String day)
    {
        this.day = day;
    }

    public static ComboList getDays()
    {
        if (days == null)
        {
            ArrayList list = new ArrayList();
            days = new ComboList();

            for (int i = 0; i < dayArray.length; i++)
            {
                DayOfWeek d = new DayOfWeek();
                d.setDayID(dayArray[i]);
                d.setDay(getDayStringArray().get(i));
                list.add(d);
            }
            days.setOptions(list);
        }
        return days;
    }

    @Override
    public String toString()
    {
        if (dayID == 0)
        {
            return getDayStringArray().get(0);
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 1; i < dayArray.length; i++)
        {
            if ((dayArray[i] & dayID) == dayArray[i])
            {
                sb.append(getDayStringArray().get(i)).append(",");
            }
        }
        if (sb.length() > 0)
        {
            return sb.substring(0, sb.length() - 1);
        }
        return sb.toString();
    }

    public static List<String> getDayStringArray()
    {
        return dayStringArray;
    }

    public  void setDayStringArray(List<String> aDayStringArray)
    {
        dayStringArray = aDayStringArray;
    }
}
